<template>
        <el-container class="home-container">
            <!-- 头部区域 -->
            <el-header>
                <div>
                    <img src="../assets/heima.png" alt="">
                    <span>电商后台管理系统</span>
                </div>
                <el-button type="info" @click="logout">退出</el-button>
            </el-header>
            <!-- 内容区域 -->
            <el-container>
                <!-- 侧边栏区域 -->
                <el-aside :width="isCollapse?'64px':'200px'">
                    <div class="toggle-button" @click="toggleCollapse">|||</div>
                    <!-- 菜单栏区域 -->
                    <el-menu
                        background-color="#333744"
                        text-color="#fff"
                        active-text-color="#409eff"
                        :unique-opened="true"
                        :collapse="isCollapse" :default-active="navState"
                        :collapse-transition="false" router>
                        <!-- 一级菜单区域 -->
                        <el-submenu :index="'/'+item.path" v-for="item in menuList" :key="item.id">
                            <template slot="title">
                            <i :class="iconList[item.id]"></i>
                            <span>{{item.authName}}</span>
                            </template>
                            <!-- 二级菜单区域 -->
                            <el-menu-item @click="saveNavState('/'+menuItem.path)" :index="'/'+menuItem.path" v-for="menuItem in item.children" :key="menuItem.id">
                                <template slot="title">
                                    <i class="el-icon-menu"></i>
                                    <span>{{menuItem.authName}}</span>
                                </template>
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <!-- 主体区域 -->
                <el-main>
                    <!-- 路由占位符 -->
                    <router-view></router-view>
                </el-main>
            </el-container>
        </el-container>
</template>

<script>
export default {
    data() {
      return {
        menuList: [],
        iconList: {
            125: 'iconfont icon-user',
            103: 'iconfont icon-tijikongjian',
            101: 'iconfont icon-shangpin',
            102: 'iconfont icon-danju',
            145: 'iconfont icon-baobiao'
        },
        isCollapse: false,
        navState: ''
      }
    },
    // a：调用接口获取左侧菜单数据
    // a1:在created阶段获取数据
    created () {
        // a2:调用方法中的获取menu菜单的方法
        this.getMenuList()
        this.navState = window.sessionStorage.getItem('navState')
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        async getMenuList () {
            // a3:调用接口获取数据
            const { data: res } = await this.$html.get('menus')
            if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
            this.menuList = res.data
        },
        toggleCollapse () {
            this.isCollapse = !this.isCollapse
        },
        // b:点击子菜单高亮实现
        saveNavState(navState) {
            window.sessionStorage.setItem('navState', navState)
            this.navState = navState
        }
    }
}
</script>

<style lang="less" scoped>
.home-container {
    height: 100%;
}
.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    color: #fff;
    font-size: 20px;
    > div {
        display: flex;
        align-items: center;
        >span {
            margin-left: 15px;
        }
    }
}
.el-aside {
    .toggle-button {
        background-color:#4a5064;
        color: #fff;
        text-align: center;
        font-size: 10px;
        line-height: 24px;
        cursor: pointer;
        letter-spacing: 0.2em;
    }
    .el-menu {
        border-right: none;
    }
    background-color: #333744;
    .iconfont {
        margin-right: 10px;
    }
}
.el-main {
    background-color: #eaedf1;
}

</style>